extends Node

## 管理抽奖和猜拳

## 可抽奖次数
var raffle_count = 1
## 可猜拳次数
var rocket_count = 0
## 猜拳赢的次数
var rocket_victory_count = 0
## 漩涡选择次数
var select_portal_count = 0
## 打金币次数
var attack_gold_count = 0
## 翻牌次数
var draw_card_count = 0
## 杀鸡次数
var kill_chicken_count = 0


const ROCK_PAPER_SCISSORS = preload("res://scenes/stages/little_game(小游戏)/猜拳/rock_paper_scissors.tscn")
const DRAW_CRADS = preload("res://scenes/stages/little_game(小游戏)/抽卡牌/draw_cards.tscn")
var reward_info
var reward_result
## 奖励类型枚举
enum RewardType {
	GOLD,      # 金币
	SKILL,     # 技能
	EQUIPMENT  # 装备
}

## 关卡奖励配置 - 随着关卡深入奖励会更好
const LEVEL_REWARDS = {
	1: {"gold": 50, "skill_weight": 1, "equipment_weight": 0.0, "gold_weight": 0.0},
	2: {"gold": 75, "skill_weight": 0.6, "equipment_weight": 0.3, "gold_weight": 0.1},
	3: {"gold": 100, "skill_weight": 0.55, "equipment_weight": 0.35, "gold_weight": 0.1},
	4: {"gold": 125, "skill_weight": 0.55, "equipment_weight": 0.35, "gold_weight": 0.1},
	5: {"gold": 150, "skill_weight": 0.5, "equipment_weight": 0.4, "gold_weight": 0.1},
	6: {"gold": 175, "skill_weight": 0.5, "equipment_weight": 0.4, "gold_weight": 0.1},
	7: {"gold": 200, "skill_weight": 0.45, "equipment_weight": 0.45, "gold_weight": 0.1},
	8: {"gold": 225, "skill_weight": 0.45, "equipment_weight": 0.45, "gold_weight": 0.1},
	9: {"gold": 250, "skill_weight": 0.4, "equipment_weight": 0.5, "gold_weight": 0.1},
	10: {"gold": 275, "skill_weight": 0.4, "equipment_weight": 0.5, "gold_weight": 0.1},
	11: {"gold": 300, "skill_weight": 0.35, "equipment_weight": 0.55, "gold_weight": 0.1},
	12: {"gold": 325, "skill_weight": 0.35, "equipment_weight": 0.55, "gold_weight": 0.1},
	13: {"gold": 350, "skill_weight": 0.3, "equipment_weight": 0.6, "gold_weight": 0.1},
	14: {"gold": 375, "skill_weight": 0.3, "equipment_weight": 0.6, "gold_weight": 0.1},
	15: {"gold": 400, "skill_weight": 0.25, "equipment_weight": 0.65, "gold_weight": 0.1}
}

## 抽奖结果结构
class RaffleResult:
	var reward_type: RewardType
	var reward_name: String
	var reward_amount: int
	var reward_description: String
	
	func _init(type: RewardType, name: String, amount: int, description: String):
		reward_type = type
		reward_name = name
		reward_amount = amount
		reward_description = description

## boss死后
func boss_die():
	raffle_count += 1
	var index = [0, 1, 2, 3, 4]
	if rocket_count >= 1:
		index.erase(0)
	if select_portal_count >= 1:
		index.erase(1)
	if attack_gold_count >= 1:
		index.erase(2)
	if draw_card_count >= 1:
		index.erase(3)
	if kill_chicken_count >= 1:
		index.erase(4)
	if index.is_empty():
		return
	match index.pick_random():
		0: rocket_count += 1
		1: select_portal_count += 1
		2: attack_gold_count += 1
		3: draw_card_count += 1
		4: kill_chicken_count += 1

## 抽奖
func raffle() -> RaffleResult:
	var current_level = Utils.now_level
	var level_config = LEVEL_REWARDS.get(current_level, LEVEL_REWARDS[15])  # 默认使用最高关卡配置
	
	# 根据权重随机选择奖励类型
	var reward_type = _get_random_reward_type(level_config)
	var skill_level = _calculate_skill_level(current_level)
	var rarity = _calculate_equipment_rarity(current_level)

	match reward_type:
		RewardType.GOLD:
			reward_result =  _get_gold_reward(level_config.gold)
		RewardType.SKILL:
			reward_result =  _get_skill_reward(skill_level)
		RewardType.EQUIPMENT:
			reward_result =  _get_equipment_reward(rarity)

	return reward_result
	
## 根据权重随机选择奖励类型
func _get_random_reward_type(level_config: Dictionary) -> RewardType:
	var rng = RandomNumberGenerator.new()
	var total_weight = level_config.skill_weight + level_config.equipment_weight + level_config.gold_weight
	var random_value = rng.randf_range(0, total_weight)
	
	if random_value < level_config.skill_weight:
		return RewardType.SKILL
	elif random_value < level_config.skill_weight + level_config.equipment_weight:
		return RewardType.EQUIPMENT
	else:
		return RewardType.GOLD

## 获取金币奖励
func _get_gold_reward(gold_amount) -> RaffleResult:
	Utils.set_player_stats_value("gold", gold_amount)
	
	return RaffleResult.new(
		RewardType.GOLD,
		"金币",
		gold_amount,
		"获得 " + str(gold_amount) + " 金币"
	)

## 获取技能奖励
func _get_skill_reward(skill_level: int) -> RaffleResult:
	# 添加等级
	var skill_name = SkillManager.get_random_skill(skill_level)

	return RaffleResult.new(
		RewardType.SKILL,
		skill_name,
		skill_level,
		"获得技能: " + skill_name + " (等级 " + str(skill_level) + ")"
	)

## 获取装备奖励
func _get_equipment_reward(rarity: int) -> RaffleResult:
	# 根据关卡获取装备稀有度
	var equipment: PandoraEntity = ItemManager.item_pool.get_random_unit(rarity)
	var equipment_name = equipment.get_string("item_name")
	var equipment_description = equipment.get_string("description")
	
	## 因为不是商店
	EventBus.push_event("add_item", [equipment])
	# 将装备添加到玩家背包
	Utils.player.player_stats.get_array("player_item_list").append(equipment)
	
	return RaffleResult.new(
		RewardType.EQUIPMENT,
		equipment_name,
		1,
		"获得装备: " + equipment_name + "\n" + equipment_description
	)

## 根据关卡计算技能等级
func _calculate_skill_level(current_level: int) -> int:
	# 基础等级为1，每3关增加1级
	var base_level = 1
	var level_bonus = ceilf(current_level - 1) / 3
	return base_level + level_bonus

## 根据关卡计算装备稀有度
func _calculate_equipment_rarity(current_level: int) -> int:
	# 根据关卡返回装备稀有度
	if current_level <= 3:
		return 1
	elif current_level <= 6:
		return 2
	elif current_level <= 9:
		return 3
	elif current_level <= 12:
		return 4
	else:
		return 4  # 最高稀有度

## 获取当前关卡的奖励配置信息
func get_level_reward_info() -> Dictionary:
	var current_level = Utils.now_level
	var config = LEVEL_REWARDS.get(current_level, LEVEL_REWARDS[15])
	
	reward_info = {
		"level": current_level,
		"gold_amount": config.gold,
		"skill_chance": config.skill_weight,
		"equipment_chance": config.equipment_weight,
		"gold_chance": config.gold_weight
	}
	return reward_info
	
## 显示抽奖结果
func show_raffle_result(result: RaffleResult) -> void:
	var message = "抽奖结果: " + result.reward_description
	Utils.show_string_label_c(message, Utils.player, Color.YELLOW)
	print("抽奖结果: ", result.reward_name, " - ", result.reward_description)

## 测试抽奖系统
func test_raffle_system() -> void:
	print("=== 测试抽奖系统 ===")
	
	# 测试不同关卡的奖励配置
	for level in range(1, 16):
		Utils.now_level = level
		var temp_reward_info = get_level_reward_info()
		print("关卡 ", level, ": 金币=", temp_reward_info.gold_amount, 
			  " 技能概率=", temp_reward_info.skill_chance * 100, "%",
			  " 装备概率=", temp_reward_info.equipment_chance * 100, "%",
			  " 金币概率=", temp_reward_info.gold_chance * 100, "%")
	
	# 测试抽奖
	Utils.now_level = 5
	var result = raffle()
	print("抽奖测试结果: ", result.reward_name, " - ", result.reward_description)
	print("=== 测试完成 ===")

## 处理输入测试
func _input(event: InputEvent) -> void:
	if event is InputEventKey and event.pressed:
		if event.keycode == KEY_F8:
			test_raffle_system()
		elif event.keycode == KEY_F9:
			# 测试单次抽奖
			var result = raffle()
			print("单次抽奖测试: ", result.reward_name, " - ", result.reward_description)
			show_raffle_result(result)

## 实例化简单石头布场景
func show_rock_paper_scissors():
	var tscn = ROCK_PAPER_SCISSORS.instantiate()
	RaffleManager.rocket_count -= 1	
	get_tree().get_nodes_in_group("ui").get(0).add_child(tscn)
	
## 实例化抽卡场景
func show_draw_card():
	var tscn = DRAW_CRADS.instantiate()
	RaffleManager.draw_card_count -= 1
	get_tree().get_nodes_in_group("ui").get(0).add_child(tscn)
